﻿using CanDichVu.Db;
using CanDichVu.Service.Iservice;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CanDichVu.Service.Vervices
{
    public class PostSer : IPost
    {
        private readonly EntitiesCanDichVu dbContext = new EntitiesCanDichVu();

        public int addorUpdate(Db.Post entity)
        {
            try
            {
                dbContext.Entry(entity).State = entity.Id == 0 ? System.Data.EntityState.Added : System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return entity.Id;
            }
            catch
            {
                return 0;
            }
        }

        public List<Db.Post> getListByChuyenMuc(int cateid, int take)
        {
            try
            {
                if (cateid != 0)
                {
                    List<int> _li = new List<int>();
                    var _en = dbContext.ChuyenMucs.Where(p => p.Id == cateid).First();
                    if (_en.ParentId == 0)
                    {
                        _li.AddRange(dbContext.ChuyenMucs.Where(p => p.ParentId == cateid).Select(c => c.Id).ToList());
                    }
                    else
                    {
                        _li.Add(cateid);
                    }

                    int[] intarray = _li.ToArray();

                    return dbContext.Posts.Where(p => intarray.Contains(p.chuyenmuc.Value)).OrderByDescending(p => p.NgayDang).Take(take).ToList();
                }
                else
                {
                    return dbContext.Posts.OrderByDescending(p => p.NgayDang).Take(take).ToList();
                }
            }
            catch
            {
                return null;
            }
        }

        public List<Db.Post> getListBythanhvien(int thanhvien, int take)
        {
            try
            {
                return dbContext.Posts.Where(p => p.UserId == thanhvien).Take(take).ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<Db.Post> getListByQuanhuyen(int quanhuyenId, int take)
        {
            try
            {
                return dbContext.Posts.Where(p => p.Quanhuyen == quanhuyenId).Take(take).ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<Db.Post> getListBytinhthanh(int TinhThanhId, int take)
        {
            try
            {
                return dbContext.Posts.Where(p => p.TinhThanh == TinhThanhId).Take(take).ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<Db.Post> Search(string cateId, int quanhuyen, int tinhthanh, string name, int take)
        {
            try
            {
                var list_search =  dbContext.Posts.Where(p => (p.ArrayCate.Contains(cateId) || cateId == "") && (p.Quanhuyen == quanhuyen || quanhuyen == 0) && (p.TinhThanh == tinhthanh || tinhthanh == 0)).ToList();

                if (name != "")
                {
                    list_search = list_search.Where(p => p.NameSearch.Contains(name)).ToList();
                }

                return list_search;
            }
            catch
            {
                return null;
            }
        }

        public Db.Post GetById(int id)
        {
            try
            {
                return dbContext.Posts.Where(p => p.Id == id).First();
            }
            catch { return null; }
        }

        public bool detele(int id)
        {
            try
            {
                var _entity = GetById(id);
                dbContext.Posts.Remove(_entity);
                dbContext.SaveChanges();
                return true;
            }
            catch { return false; }
        }

        public int capnhapView(int id, int view)
        {
            try
            {
                var _entity = GetById(id);
                _entity.LuotView = _entity.LuotView + view;
                dbContext.SaveChanges();
                return _entity.LuotView.Value;
            }
            catch { return 0; }
        }

        public int capnhatLike(int id, int like)
        {
            try
            {
                var _entity = GetById(id);
                _entity.LuotView = _entity.LuotLike + like;
                dbContext.SaveChanges();
                return _entity.LuotLike.Value;
            }
            catch { return 0; }
        }

        public int capnhatNgaySua(int id, DateTime datengaysua)
        {
            try
            {
                var _entity = GetById(id);
                _entity.NgaySua = datengaysua;
                dbContext.SaveChanges();
                return _entity.LuotLike.Value;
            }
            catch { return 0; }
        }

        public int capnhattinnhanvip(int id, bool viptype, DateTime ngaynhantinvip, DateTime ngayhethan)
        {
            try
            {
                var _entity = GetById(id);
                _entity.tinnhanvip = viptype;
                _entity.NgayNhantinVip = ngaynhantinvip;
                _entity.NgayHetHan = ngayhethan;
                dbContext.SaveChanges();
                return _entity.Id;
            }
            catch { return 0; }
        }

        public List<Post> GetListPostVip(string cateId, bool type, bool checkdate, int take)
        {
            try
            {
                if (checkdate)
                {
                    return dbContext.Posts.Where(p => (p.ArrayCate.Contains(cateId) || cateId == "") && p.tinnhanvip == type && p.NgayHetHan > DateTime.Now).Take(take).ToList();
                }
                else
                {
                    return dbContext.Posts.Where(p => (p.ArrayCate.Contains(cateId) || cateId == "") && p.tinnhanvip == type && p.NgayHetHan < DateTime.Now).Take(take).ToList();
                }
                
            }
            catch
            {
                return null;
            }
        }

        public List<Post> GetListOnCate(int cateId, bool type, bool checkdate, int take, int skip)
        {
            try
            {
                List<Post> ListReturn = new List<Post>();


                List<int> _li = new List<int>();
                var _en = dbContext.ChuyenMucs.Where(p => p.Id == cateId).First();
                if (_en.ParentId == 0)
                {
                    _li.AddRange(dbContext.ChuyenMucs.Where(p => p.ParentId == cateId).Select(c => c.Id).ToList());
                }
                else
                {
                    _li.Add(cateId);
                }

                int[] intarray = _li.ToArray();

                if (checkdate)
                {
                    ListReturn = dbContext.Posts.Where(p => (intarray.Contains(p.chuyenmuc.Value) || cateId == 0) && p.tinnhanvip == type && p.NgayHetHan > DateTime.Now).ToList();
                }
                else
                {
                    ListReturn = dbContext.Posts.Where(p => (intarray.Contains(p.chuyenmuc.Value) || cateId == 0) && p.tinnhanvip == type && p.NgayHetHan < DateTime.Now).ToList();
                }

                return ListReturn.Skip(skip).Take(take).ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<Post> GetListOnTinhThanh(int cateId, bool type, bool checkdate, int take, int skip)
        {
            try
            {
                List<Post> ListReturn = new List<Post>();


                if (checkdate)
                {
                    ListReturn = dbContext.Posts.Where(p => (p.TinhThanh.Value == cateId || cateId == 0) && p.tinnhanvip == type && p.NgayHetHan > DateTime.Now).ToList();
                }
                else
                {
                    ListReturn = dbContext.Posts.Where(p => (p.TinhThanh.Value == cateId || cateId == 0) && p.tinnhanvip == type && p.NgayHetHan < DateTime.Now).ToList();
                }

                return ListReturn.Skip(skip).Take(take).ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<Post> GetListOnQuanHuyen(int cateId, bool type, bool checkdate, int take, int skip)
        {
            try
            {
                List<Post> ListReturn = new List<Post>();


                if (checkdate)
                {
                    ListReturn = dbContext.Posts.Where(p => (p.Quanhuyen == cateId || cateId == 0) && p.tinnhanvip == type && p.NgayHetHan > DateTime.Now).ToList();
                }
                else
                {
                    ListReturn = dbContext.Posts.Where(p => (p.Quanhuyen == cateId || cateId == 0) && p.tinnhanvip == type && p.NgayHetHan < DateTime.Now).ToList();
                }

                return ListReturn.Skip(skip).Take(take).ToList();
            }
            catch
            {
                return null;
            }
        }

        public bool checkPostOfAccount(int postId, int accountId)
        {
            try
            {
                return true;
            }
            catch { return false; }
        }
    }
}
